package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.InfoTypeMapper;
import com.link_with_health.mapper.SysInfoMapper;
import com.link_with_health.model.InfoTypeEntity;
import com.link_with_health.model.SysInfoEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.SysInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("sysInfoService")
public class SysInfoServiceImpl extends BaseService implements SysInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    SysInfoMapper sysInfoMapper;
    @Resource
    InfoTypeMapper infoTypeMapper;

    /**
     * 通过通知信息id获取通知信息
     * @param id 通知信息id
     * @return  通知信息
     * @throws BaseException 异常
     */
    @Override
    public SysInfoEntity getSysInfoById(Long id) throws BaseException {
        SysInfoEntity entity = null;
        try {
            entity = sysInfoMapper.querySysInfoById(id);
        } catch (Exception e) {
            logger.error("[query sysInfo failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("SYSINFO.ID: "+id);

        return entity;
    }

    /**
     * 通过发送者获取通知信息
     * @param id 发送者id
     * @return 通知信息
     * @throws BaseException 异常
     */
    @Override
    public List<SysInfoEntity> getSysInfoBySender(Long id) throws BaseException {
        List<SysInfoEntity> sysInfoList = null;
        try {
            sysInfoList = sysInfoMapper.querySysInfoBySender(id);
        } catch (Exception e){
            logger.error("[query sysInfo failed]",e);
        }
        if (sysInfoList == null || sysInfoList.isEmpty())
            throw new NullQueryResultException("SYSINFO.SENDER: "+id);
        return sysInfoList;
    }

    /**
     * 通过接受者id获取通知信息
     * @param id 接受者id
     * @return 通知信息
     * @throws BaseException 异常
     */
    @Override
    public List<SysInfoEntity> getSysInfoByReceiver(Long id) throws BaseException {
        List<SysInfoEntity> sysInfoList = null;
        try {
            sysInfoList = sysInfoMapper.querySysInfoByReceiver(id);
        } catch (Exception e) {
            logger.error("[query sysInfo failed]",e);
        }

        if (sysInfoList == null || sysInfoList.isEmpty())
            throw new NullQueryResultException("SYSINFO.RECEIVER:");
        return sysInfoList;
    }

    /**
     * 通过信息类型获取通知信息
     * @param id 类型id
     * @return 通知信息
     * @throws BaseException 异常
     */
    @Override
    public List<SysInfoEntity> getSysInfoByInfoType(int id) throws BaseException {
        List<SysInfoEntity> sysInfoList = null;
        try {
            sysInfoList = sysInfoMapper.querySysInfoByInfoType(id);
        } catch (Exception e) {
            logger.error("[query sysInfo failed]",e);
        }
        return sysInfoList;
    }

    /**
     * 获取所有通知信息
     * @return 通知信息
     * @throws BaseException 异常
     */
    @Override
    public List<SysInfoEntity> getAllSysInfo() throws BaseException {
        List<SysInfoEntity> sysInfoList = null;
        try {
            sysInfoList = sysInfoMapper.queryAllSysInfo();
        } catch (Exception e) {
            logger.error("[query sysInfo failed]",e);
        }
        if (sysInfoList == null || sysInfoList.isEmpty())
            throw new NullQueryResultException("ALL SYSINFO");
        return sysInfoList;
    }

    /**
     * 增加通知信息
     * @param sysInfo 通知信息
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addSysInfo(SysInfoEntity sysInfo) throws BaseException {
        int result = 0;
        try {
            result = sysInfoMapper.insertSysInfo(sysInfo);
        } catch (Exception e) {
            logger.error("[add sysInfo failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT SYSYINFO: "+sysInfo);
        return result;
    }

    /**
     * 修改通知信息
     * @param sysInfo 通知信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeInfo(SysInfoEntity sysInfo) throws BaseException {
        int result = 0;
        try {
            result = sysInfoMapper.updateSysInfo(sysInfo);
        } catch (Exception e) {
            logger.error("[change sysInfo failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE SYSINFO: "+sysInfo);
        return result;
    }

    /**
     * 删除通知信息
     * @param id 信息id
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deleteSysInfo(Long id) throws BaseException {
        int result = 0;
        try {
            result = sysInfoMapper.deleteSysInfo(id);
        } catch (Exception e) {
            logger.error("[delete sysInfo failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("SYSINFO.ID: "+id);

        return result;
    }

    /**
     * 通过类型id获取类型信息
     * @param id 类型id
     * @return  类型数据
     * @throws BaseException 异常
     */
    @Override
    public InfoTypeEntity getInfoTypeById(int id) throws BaseException {
        InfoTypeEntity entity = null;
        try {
            entity = infoTypeMapper.queryInfoTypeById(id);
        } catch (Exception e) {
            logger.error("[query infoType failed]",e);
        }

        if (entity == null)
            throw new NullQueryResultException("INFOTYPE.TYPE");
        return entity;
    }

    /**
     * 通过类型名称获取类型信息 支持模糊
     * @param type 名称
     * @return 类型信息
     * @throws BaseException 异常
     */
    @Override
    public List<InfoTypeEntity> getInfoTypeByType(String type) throws BaseException {
        List<InfoTypeEntity> infoTypeList = null;
        try {
            infoTypeList = infoTypeMapper.queryInfoTypeByType("%"+type+"%");
        } catch (Exception e) {
            logger.error("[query infoType failed]",e);
        }
        if (infoTypeList == null || infoTypeList.isEmpty())
            throw new NullQueryResultException("INFOTYPE.TYPE: "+type);

        return infoTypeList;
    }

    /**
     * 获取所有类型
     * @return  类型信息
     * @throws BaseException 异常
     */
    @Override
    public List<InfoTypeEntity> getAllInfoType() throws BaseException {
        List<InfoTypeEntity> infoTypeList = null;
        try {
            infoTypeList = infoTypeMapper.queryAllInfoType();
        } catch (Exception e) {
            logger.error("[query infoType failed]",e);
        }
        if (infoTypeList == null || infoTypeList.isEmpty())
            throw new NullQueryResultException("ALL INFOTYPE");

        return infoTypeList;
    }

    /**
     * 添加类型信息
     * @param infoType 类型信息
     * @return 类型信息
     * @throws BaseException 异常
     */
    @Override
    public int addInfoType(InfoTypeEntity infoType) throws BaseException {
        int result = 0;
        try {
            result = infoTypeMapper.insertInfoType(infoType);
        } catch (Exception e) {
            logger.error("[add infoType failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INFOTYPE: "+infoType);
        return result;
    }

    /**
     * 修改类型
     * @param infoType 通知信息
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeInfoType(InfoTypeEntity infoType) throws BaseException {
        int result = 0;
        try {
            result = infoTypeMapper.updateInfoType(infoType);
        } catch (Exception e) {
            logger.error("[change infoType failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INFOTYPE.type");
        return result;
    }

    /**
     * 删除类型
     * @param id 类型id
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deleteInfoType(int id) throws BaseException {
       int result = 0;
       try {
           result = infoTypeMapper.deleteInfoType(id);
       } catch (Exception e) {
           logger.error("[delete infoType failed]",e);
       }if (result == 0)
           throw new ModifyDatabaseFailedException("DELETE INFOTYPE.ID: "+id);
        return result;
    }
}
